package com.td.filemanager.business.device;

import com.td.filemanager.ResourceTable;
import com.td.filemanager.business.filetype.FileManagerAbilitySlice;
import com.td.filemanager.provider.ListComponentProvider;
import com.td.filemanager.slice.BaseAbilitySlice;
import com.td.lib.viewmodel.DefaultDataObserver;
import com.td.lib.viewmodel.ViewModelAbility;
import com.td.lib.viewmodel.ViewModelProvider;
import com.td.library.data.DeviceData;
import com.td.library.utils.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.List;


/**
 * 设备管理页面
 */
public class DeviceManagerAbilitySlice extends BaseAbilitySlice {
    private static final String TAG = DeviceManagerAbilitySlice.class.getSimpleName();

    private ListContainer mListContainer;
    private Component mConfirm;
    private Checkbox mSelectAllComponent;
    private Component mSeleckAllContainer;
    private Component mRefresh;
    private DeviceDataProvider mDeviceDataProvider;

    private DeviceManagerViewModel mViewModel;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_slice_device_managner);
        initViewModel();
        initComponent();
        initListener();
        initObserver();
    }


    private void initComponent() {
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container_device);
        mConfirm = findComponentById(ResourceTable.Id_confirm);
        mRefresh = findComponentById(ResourceTable.Id_refresh);
        mSelectAllComponent = (Checkbox) findComponentById(ResourceTable.Id_seleckAll);
        mSeleckAllContainer = findComponentById(ResourceTable.Id_seleckAllContainer);
    }

    private void initListener() {
        mSeleckAllContainer.setClickedListener(component -> {
            LogUtil.info(TAG, "mSeleckAllContainer setClickedListener" );
            mSelectAllComponent.setChecked(!mSelectAllComponent.isChecked());

        });
        mSelectAllComponent.setCheckedStateChangedListener((absButton, data) -> {
            LogUtil.info(TAG, "onCheckedChanged " + data);
            mViewModel.getSelectAll().setData(data);
        });
        mConfirm.setClickedListener(component -> {
            present(new FileManagerAbilitySlice(), new Intent());
        });

        mRefresh.setClickedListener(component -> {
            mViewModel.refresh();
        });
    }


    private void initViewModel() {
        if (getAbility() instanceof ViewModelAbility) {
            ViewModelAbility viewModelAbility = (ViewModelAbility) getAbility();
            mViewModel = new ViewModelProvider(viewModelAbility.getViewModelStore())
                    .get(DeviceManagerViewModel.class);
        }
    }

    private void initObserver() {
        mViewModel.getDevicedata().addObserver(
                new DefaultDataObserver<List<DeviceData>>(getLifecycle()) {
            @Override
            public void onChanged(List<DeviceData> deviceData) {
                LogUtil.info(TAG, "onChanged getDevicedata " + deviceData);
                if (mDeviceDataProvider == null) {
                    initDeviceDataProvider(deviceData);
                } else {
                    mDeviceDataProvider.notifyDataChanged();
                }
                boolean selectAll = deviceData.stream().allMatch(DeviceData::isChecked);
                mSelectAllComponent.setChecked(selectAll);
            }
        }, false);

        mViewModel.getSelectAll().addObserver(new DefaultDataObserver<Boolean>(getLifecycle()) {
            @Override
            public void onChanged(Boolean aBoolean) {
                LogUtil.info(TAG, "DeviceManagerAbilitySlice onChanged getSelectAll " + aBoolean);
                mSelectAllComponent.setChecked(aBoolean);
                List<DeviceData> deviceData = mViewModel.getDevicedata().getData();
                deviceData.stream().forEach(device -> {
                    device.setChecked(aBoolean);
                });
                mViewModel.getDevicedata().setData(deviceData);
            }
        }, false);
    }

    private void initDeviceDataProvider(List<DeviceData> deviceData) {
        mDeviceDataProvider = new DeviceDataProvider(DeviceManagerAbilitySlice.this,
                deviceData, ResourceTable.Layout_dialog_device_item);
        mListContainer.setItemProvider(mDeviceDataProvider);

        mDeviceDataProvider.setOnItemClickListener(new ListComponentProvider.OnItemClickListener<DeviceData>() {
            @Override
            public void onItemClick(Component component, DeviceData item, int position) {
                item.setChecked(!item.isChecked());
                mDeviceDataProvider.notifyDataSetItemChanged(position);
                if (mSelectAllComponent.isChecked() && mViewModel.isAllItemUnSelected()) {
                    mSelectAllComponent.setChecked(false);
                }
                if (!mSelectAllComponent.isChecked() && mViewModel.isAllItemSelected()) {
                    mSelectAllComponent.setChecked(true);
                }
                LogUtil.info(TAG, "onItemClick " + position + " item " + item);
            }
        });
        mDeviceDataProvider.setOnItemCheckBoxCheckedListener(new DeviceDataProvider.OnItemCheckBoxCheckedListener() {
            @Override
            public void onItemCbCheckedChangedListener(int itemPosition, boolean isChecked) {
                LogUtil.warn(TAG, "onItemCbCheckedChangedListener " + itemPosition + " isChecked " + isChecked);
                if (mViewModel != null) {
                    mViewModel.refreshItemCbState(itemPosition, isChecked);
                }
            }
        });
    }
}
